<!-- src/components/Grades.vue -->
<template>
  <div>

    <div class="search-and-sort">
      <el-input placeholder="请输入内容" v-model="search" class="input-with-select">
        <el-button slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <el-select v-model="sort" placeholder="排序" class="sort-select">
        <el-option label="ID升序" value="idAsc"></el-option>
        <el-option label="ID降序" value="idDesc"></el-option>
      </el-select>
    </div>

    <el-table :data="filteredGrades" style="width: 100%">
      <el-table-column prop="pkCid" label="ID" width="100"></el-table-column>
      <el-table-column prop="courseName" label="课程名" width="180"></el-table-column>
      <el-table-column prop="startTime" label="上课时间" width="180"></el-table-column>
      <el-table-column prop="maxStudents" label="最大人数" width="100"></el-table-column>
      <el-table-column prop="credit" label="学分" width="100"></el-table-column>
      <el-table-column label="操作" width="240">
        <template slot-scope="scope">
          <el-button type="text" @click="viewStudentGrades(scope.row.pkCid)">查看学生成绩</el-button>
          <el-button type="text" @click="viewGradeAnalysis(scope.row)">查看成绩分析</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'TeacherGradesContent',
  data() {
    return {
      grades: [],
      search: '',
      sort: '',
      currentPage: 1,
      pageSize: 10,
      total: 0
    };
  },
  computed: {
    filteredGrades() {
      let filtered = this.grades.filter(grade => {
        return grade.courseName.toLowerCase().includes(this.search.toLowerCase());
      });

      if (this.sort === 'idAsc') {
        filtered.sort((a, b) => a.pkCid - b.pkCid);
      } else if (this.sort === 'idDesc') {
        filtered.sort((a, b) => b.pkCid - a.pkCid);
      }

      this.total = filtered.length;
      return filtered.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize);
    }
  },
  created(){
    this.fetchGrades()
  },
  methods: {
    fetchGrades(){
      let userId = localStorage.getItem("userId")
        axios.get("/api/teacher/courses?userId="+userId).then(response => {
          this.grades = response.data.list
        }).catch( err=> {
          this.$message.error("加载异常")
        })
    },
    viewStudentGrades(courseId) {
      this.$router.push({ name: 'StudentGrades', params: { courseId: courseId } });
    },
    viewGradeAnalysis(course) {
      this.$router.push({ name: 'GradeAnalysis', params: { course: course} });
    },
    handleSizeChange(val) {
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    }
  }
};
</script>

<style scoped>
.breadcrumb {
  margin-bottom: 20px;
}

.search-and-sort {
  display: flex;
  margin-bottom: 20px;
}

.input-with-select {
  width: 300px;
  margin-right: 20px;
}

.sort-select {
  width: 120px;
}

.el-pagination {
  margin-top: 20px;
}
</style>
